import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from Day_2211A.Unit_14.mysql import Mysql

class A3mall(unittest.TestCase):

    def setUp(self):
        self.sql = Mysql('10.59.9.12', 'root', 'yidong666', 'a3mall')

    def tearDown(self):
        self.sql.close()

    ddh = None
    def test_login_01(self):
        self.driver = webdriver.Edge()
        self.driver.get('http://10.59.9.12:7001/')
        time.sleep(2)
        # 登录账号
        self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
        time.sleep(2)
        # 输入账号
        self.driver.find_element(By.NAME, 'username').send_keys('15523615757')
        # 输入密码
        self.driver.find_element(By.NAME, 'password').send_keys('123456')
        time.sleep(10)
        # 点击登录
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[1]/div[2]/form/div[5]/a').click()
        time.sleep(10)
        # 提取登录后返回的用户信息
        dy1 = self.driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').text
        print(dy1)
        # 查询数据库获取用户名
        dy2 = self.sql.chaxun("select username from mall_users where username = '15523615757'")
        print(dy2)
        self.assertEqual(dy1,dy2)
        # 账户余额
        dy7 = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[4]/span').text
        qq = dy7.split('¥ ')
        amount = float(qq[1])
        print(amount)
        # 查询数据库用户余额
        b = self.sql.chaxun('select amount from mall_users where username = 15523615757')
        money = float(b[0][0])
        print(money)
        # 数据库中用户的余额和个人中心的余额是否相等
        self.assertEqual(amount,money)
        # 点击团购
        self.driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/ul/li[5]/a').click()
        time.sleep(3)
        # 点击商品
        self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[2]/div[1]/ul/li[2]/div[1]/span/a/img').click()
        time.sleep(3)
        # 获取商品价格
        price = self.driver.find_element(By.ID, 'sell-price-box').text
        A3mall.price = float(price)
        print(A3mall.price)
        # 断言商品数量
        num1 = self.a.execute('select store_nums from mall_goods where id=33')
        num2 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div/div[2]/form/div/div[4]/div[1]/div[2]/span[3]/i').text
        self.assertEqual(int(num1[0][0]), int(num2))
        A3mall.num = int(num1[0][0])
        # 断言商品数量是否正确
        dy3 = self.driver.find_element(By.XPATH, '/html/body/div[4]/form/div[4]/div[1]/table/tbody/tr[2]/td[3]').text
        print(dy3)
        self.assertEqual('3', dy3)
        # 立即购买
        self.driver.find_element(By.CLASS_NAME, 'shop-btn').click()
        time.sleep(3)
        # 选择收货地址
        self.driver.find_element(By.XPATH, '/html/body/div[4]/form/div[2]/div[2]/div[1]/label/input').click()
        time.sleep(2)
        self.driver.find_element(By.XPATH, '/html/body/div[4]/form/div[1]/div[2]/div/label/input').click()
        time.sleep(3)
        # 选择支付方式 余额
        self.driver.find_element(By.XPATH, '/html/body/div[4]/form/div[3]/div[2]/div[2]/label/input').click()
        time.sleep(2)
        self.driver.find_element(By.XPATH, '/html/body/div[4]/form/div[3]/div[2]/div[1]/label/input').click()
        time.sleep(3)
        # 滑动
        self.driver.execute_script("window.scrollTo(0,3000)")
        time.sleep(2)
        # 获取运费
        num = self.driver.find_element(By.ID, 'real-freight').text
        A3mall.freight = float(num)
        print(A3mall.freight)
        # 下一步
        self.driver.find_element(By.ID, 'order-button').click()
        time.sleep(3)
        # 查看订单
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/p/a').click()
        time.sleep(3)
        dy4 = self.driver.find_element(By.CLASS_NAME, 'succe-ico').text
        print(dy4)
        self.assertEqual("您的订单支付成功", dy4)


    def test_jf_02(self):
        self.driver = webdriver.Edge()
        self.driver.get('http://10.59.9.12:7001/')
        time.sleep(2)
        # 登录账号
        self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
        time.sleep(2)
        # 输入账号
        self.driver.find_element(By.NAME, 'username').send_keys('15523615757')
        # 输入密码
        self.driver.find_element(By.NAME, 'password').send_keys('123456')
        time.sleep(10)
        # 点击登录
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[1]/div[2]/form/div[5]/a').click()
        time.sleep(10)
        # 点击积分商城
        self.driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/ul/li[6]/a').click()
        time.sleep(3)
        # 选择第一个商品
        self.driver.find_element(By.CLASS_NAME, 'lazy').click()
        time.sleep(3)
        # 提取商品需要的积分
        jf = self.driver.find_element(By.CLASS_NAME, 'rates-number').text
        # 点击立即兑换
        self.driver.find_element(By.ID, 'buy-cart').click()
        time.sleep(2)
        # 选择地址
        self.driver.find_element(By.ID,'address-65').click()
        time.sleep(2)
        # 选择支付方式
        self.driver.find_element(By.CLASS_NAME, 'radio-select-btn').click()
        time.sleep(2)
        # 下一步
        self.driver.find_element(By.ID, 'order-button').click()
        time.sleep(2)
        # 您的订单支付成功，文本断言
        dy = self.driver.find_element(By.CLASS_NAME, 'succe-ico').text
        self.assertEqual('您的订单支付成功', dy)
        # 查看订单
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/p/a').click()
        time.sleep(2)


if __name__ == '__main__':
    unittest.main()